from django.shortcuts import render,redirect,reverse
from django.db import connection
from django.views.decorators.csrf import csrf_exempt
#图书管理页面
def index(request):
    return render(request,'index.html')

#显示全部
def allbook(request):
    cursor = connection.cursor()  # 游标
    cursor.execute('select id,name,author,publish,price,types,pages from books')  #
    allbooks = cursor.fetchall()  #
    context = {
        "allbooks":allbooks
    }   #那全部数据到列表
    return render(request,'books.html',context=context)
@csrf_exempt
#添加
def addbook(request):
    if request.method =='GET':
        return render(request, 'addbook.html')
    else:
        list = []
        name = request.POST.get('name')
        author = request.POST.get('author')
        publish = request.POST.get('publish')
        price = request.POST.get('price')
        types = request.POST.getlist('types')
        list2=[str(i) for i in types]
        list3 = ' '.join(list2)
        pages = request.POST.get('pages')
        cursor = connection.cursor()
        sql = 'insert into books(name,author,publish,price,types,pages) values(%s,%s,%s,%s,%s,%s) '
        param = (name, author, publish, price, list3 , pages)
        cursor.execute(sql,param)
        return redirect(reverse('books'))


def delete_id(request,bookid):
    cursor = connection.cursor()
    cursor.execute('delete from books where id = %s' % bookid)
    return redirect(reverse('books'))



def update(request,bookid):

    if request.method =='GET':
        cursor = connection.cursor()  # 游标
        cursor.execute('select  * from books where id= %s' % bookid)  #
        allbooks = cursor.fetchall()  #
        context = {
            "allbooks": allbooks
        }
        return render(request,'update.html',context=context)
    else:
        cursor = connection.cursor()
        cursor.execute('select id from books where id=%s' % bookid)
        bookid = cursor.fetchone()
        name = request.POST.get('nname')
        author = request.POST.get('nauthor')
        publish = request.POST.get('npublish')
        price = request.POST.get('nprice')
        types = request.POST.get('ntypes')
        pages = request.POST.get('npages')
        if name =="":
            pass
        else:
            sql = ('update books set name = %s where id= %s'  )
            param = (name, bookid)
            cursor.execute(sql, param)
        if author =="":
            pass
        else:
            sql = ('update books set author = %s where id= %s'  )
            param = (author, bookid)
            cursor.execute(sql, param)
        if publish =="":
            pass
        else:
            sql = ('update books set publish = %s where id= %s'  )
            param = (publish, bookid)
            cursor.execute(sql, param)
        if price =="":
            pass
        else:
            sql = ('update books set price = %s where id= %s'  )
            param = (price, bookid)
            cursor.execute(sql, param)
        if types =="":
            pass
        else:
            sql = ('update books set types = %s where id= %s'  )
            param = (types, bookid)
            cursor.execute(sql, param)
        if pages =="":
            pass
        else:
            sql = ('update books set pages = %s where id= %s'  )
            param = (pages, bookid)
            cursor.execute(sql, param)
        return redirect(reverse('books'))